/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */
#include "include.h"

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr){return head;}
        
        // if (head == nullptr){return;}
        ListNode* prev = nullptr;
        ListNode* current = head;
        
        // ListNode* last; // = head
        while (current){
            ListNode* next = current->next;
            current->next = prev;
            prev = current;
            current = next;
        }

        // current has been changed to tail->next = nullptr
        // now prev is the origin tail
        return prev;

        // // recursionReverse(head, nullptr, head);

        // // head insert
        // ListNode* queueFront = head;
        // ListNode* current = head;

        // while (current->next){
        //     ListNode* next = current->next;
        //     current->next = next->next;
        //     next->next = queueFront;
        //     queueFront = next;
        // }

        // head = queueFront;

        // return head;
    }
    // void recursionReverse(ListNode* node, ListNode* from, ListNode*&head){
    //     if (node->next){
    //         recursionReverse(node->next, node, head);
    //     }else{
    //         head = node;
    //     }
    //     node->next = from;
    // }

    ListNode* insertPos;
};
// @lc code=end

int main(){
    ListNode* node3 = new ListNode(3);
    ListNode* node2 = new ListNode(2, node3);
    ListNode* node1 = new ListNode(1, node2);

    Solution test;
    test.reverseList(node1);
}

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* curr = head;
        while (curr) {
            ListNode* next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
};
